package 基础练习;/**
 * @author： li
 * @date： 2022-01-08 16:38
 * @version 1.0
 */

import java.util.Scanner;

/**
 * @author： ljm
 * @date： 2022-01-08 16:38
 * @version 1.0
 */
public class 十六进制转八进制01 {
    static String[] bin = { "0000", "0001", "0010", "0011", "0100", "0101",
            "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101",
            "1110", "1111" };
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        String[] res = new String[n];
        for(int i = 0 ;i < n ;i++){
            res[i] = scanner.next();
            res[i] = HtoO(res[i]);
        }
        for(int i = 0 ;i < n ;i++)
            System.out.println(res[i]);
    }
    public static  String HtoO(String res ){
        //十六进制转成二进制
        StringBuffer avs = new StringBuffer();
        for(int j = 0 ;j < res .length() ;j++){
            //把String（字符串）分解成char（字符）
            char c = res .charAt(j);
            if(c<='F'&&c>='A')
                avs.append(bin[c - 'A' + 10]);
            else
                avs.append(bin[c - '0']);
        }
        //二进制转八进制
        int n = 0 ;
        int num = 0 ;
        StringBuffer sb = new StringBuffer();
        for(int i = avs.length() - 1 ; i >= 0; i--){
            num +=Math.pow(2,n)*(avs.charAt(i) - '0');
            if(n == 2 || i == 0) {
                sb.append(num);
                n = 0 ;
                num = 0 ;
            }else
                n++;
        }
        while(sb.charAt(sb.length() - 1) == '0')
            sb.deleteCharAt(sb.length() - 1);
        //reverse()倒序输出
        return sb.reverse().toString();
    }

}

